package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.HealthRecord;
import com.battle.pojo.entity.SkillCertificate;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.skillCertificate.*;
import com.battle.pojo.vo.personWarCase.GetPersonWarCaseListVo;
import com.battle.pojo.vo.skillCertificate.*;
import com.battle.pojo.dto.skillCertificate.*;
import com.battle.service.repository.in.SkillCertificateRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.SkillCertificateService;
import com.battle.service.utils.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：技能证书服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class SkillCertificateServiceImpl implements SkillCertificateService {

    private final SkillCertificateRepository skillCertificateRepository;
    private final UserRepository userRepository;

    public SkillCertificateServiceImpl(SkillCertificateRepository skillCertificateRepository, UserRepository userRepository) {
        this.skillCertificateRepository = skillCertificateRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建技能证书
     *
     * @param param
     */
    @Override
    public void createSkillCertificate(CreateSkillCertificateParam param) {
        //判断技能名称+技能类型是否重复
        List<SkillCertificate> skillCertificates = skillCertificateRepository.getBaseMapper().selectList(
                new LambdaQueryWrapper<SkillCertificate>()
                        .eq(SkillCertificate::getName, param.getName())
                        .eq(SkillCertificate::getType, param.getType())
                        .eq(SkillCertificate::getIsDelete, 0)
        );
        if (CollectionUtil.isNotEmpty(skillCertificates)) {
            throw new BizException("技能证书名称和类型已存在");
        }

        SkillCertificate skillCertificate = new SkillCertificate();
        BeanUtils.copyProperties(param, skillCertificate);
        Long id = IdGenerator.SNOW.generate();
        skillCertificate.setId(id);
        skillCertificateRepository.create(skillCertificate);
    }

    /**
     * 修改技能证书
     *
     * @param param
     */
    @Override
    public void editSkillCertificate(EditSkillCertificateParam param) {
        Long id = param.getId();
        SkillCertificate skillCertificate = skillCertificateRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, skillCertificate);
        skillCertificateRepository.edit(skillCertificate);
    }

    /**
     * 移除技能证书
     *
     * @param id
     */
    @Override
    public void removeSkillCertificate(Long id) {
        skillCertificateRepository.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bathUpdate() {
        List<SkillCertificate> healthRecords = skillCertificateRepository.getBaseMapper().selectList(new LambdaQueryWrapper<>());
        if (CollectionUtil.isNotEmpty(healthRecords)){
            for (SkillCertificate healthRecord : healthRecords) {
                skillCertificateRepository.updateById(healthRecord);
            }
        }
    }

    /**
     * 启动/禁用技能证书
     *
     * @param id
     */
    @Override
    public void enabledSkillCertificate(Long id) {
        SkillCertificate skillCertificate = skillCertificateRepository.getByIdCheck(id);
        skillCertificate.setIsDisable(!skillCertificate.getIsDisable());
        skillCertificateRepository.edit(skillCertificate);
    }

    /**
     * 获取技能证书详情
     *
     * @param id
     * @return
     */
    @Override
    public GetSkillCertificateVo getSkillCertificate(Long id) {
        SkillCertificate skillCertificate = skillCertificateRepository.getByIdCheck(id);
        GetSkillCertificateVo vo = new GetSkillCertificateVo();
        BeanUtils.copyProperties(skillCertificate, vo);
        return vo;
    }

    /**
     * 获取技能证书 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetSkillCertificateListVo> getSkillCertificateList(GetSkillCertificateListParam param) {

        SelectSkillCertificateDto dto = new SelectSkillCertificateDto();
        BeanUtils.copyProperties(param, dto);
        Page<SkillCertificate> page = skillCertificateRepository.selectSkillCertificate(dto);
        List<SkillCertificate> skillCertificateList = page.getRecords();
        List<GetSkillCertificateListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(skillCertificateList)) {
            List<Long> userIds = new ArrayList<>();
            skillCertificateList.forEach(v -> {
                userIds.add(v.getCreator());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);

            for (SkillCertificate skillCertificate : skillCertificateList) {
                GetSkillCertificateListVo vo = new GetSkillCertificateListVo();
                BeanUtils.copyProperties(skillCertificate, vo);

                User user = userMap.get(skillCertificate.getCreator());
                if (user!=null){
                    vo.setCreateName(user.getName());
                }

                vo.setStatus(0);
                LocalDate now = LocalDate.now();
                if (now.isAfter(skillCertificate.getStartTime()) && now.isBefore(skillCertificate.getEndTime())) {
                    vo.setStatus(1);
                }

                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 导出技能证书
     * @param param
     * @param response
     * @throws IOException
     */
    @Override
    public void export(GetSkillCertificateListParam param, HttpServletResponse response) throws IOException {
        param.setSize(Long.MAX_VALUE);
        PageResult<GetSkillCertificateListVo> pageResult = getSkillCertificateList(param);
        List<GetSkillCertificateListVo> list = pageResult.getRecords();
        ExcelUtils.exportExcel(list, "技能证书", "技能证书", GetSkillCertificateListVo.class, "技能证书",response);

    }

    /**
     * 获取技能证书下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetSkillCertificateDownListVo> getSkillCertificateDownList(GetSkillCertificateDownListParam param) {
        SkillCertificate po = new SkillCertificate();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<SkillCertificate> skillCertificateList = skillCertificateRepository.getListByEntity(po);

        List<GetSkillCertificateDownListVo> list = new ArrayList<>();
        for (SkillCertificate skillCertificate : skillCertificateList) {
            GetSkillCertificateDownListVo vo = new GetSkillCertificateDownListVo();
            BeanUtils.copyProperties(skillCertificate, vo);
            list.add(vo);
        }
        return list;
    }


}



